package com.zy.mc_module.utils;

import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created on 18/10/20
 * 时间工具类
 */
public class DateUtil {

    public static String formatTime(String time, String format) {
        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat(format);
            return sdf1.format(sdf1.parse(time));
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /***
     * 格式化此类型时间 2018-12-15T16:00:00.000+0000
     */
    public static String formatTTime(String time) {
        try {
            return time.split("T")[0];
        } catch (Exception e) {
            return time;
        }
    }

    /***
     * 格式化此类型时间 2018-12-15T16:00:00.000+0000
     */
    public static String formatTTime1(String time) {
        try {
            String yyr = time.split("T")[0];
            String sfm = time.split("T")[1].split("\\.")[0];
            return yyr + "  " + sfm;
        } catch (Exception e) {
            return time;
        }
    }

    /***
     * 格式化此类型时间 2018-12-15 16:00:00
     * 2018 年 12 月 15 日
     */
    public static String formatTime(String time) {
        try {
            String yyr = time.split(" ")[0];
            String format_yyr = yyr.split("-")[0] + "年 " + yyr.split("-")[1] + "月 " + yyr.split("-")[2] + "日 ";
            return format_yyr;
        } catch (Exception e) {
            return time;
        }
    }

    /***
     * 格式化此类型时间 2018-12-15T16:00:00.000+0000
     * 2018 年 12 月 15 日 16:00:00
     */
    public static String formatTTTime(String time) {
        try {
            String yyr = time.split("T")[0];
            String format_yyr = yyr.split("-")[0] + " 年 " + yyr.split("-")[1] + " 月 " + yyr.split("-")[2] + " 日 ";
            return format_yyr + time.split("T")[1].substring(0, 8);
        } catch (Exception e) {
            return time;
        }
    }

    /***
     * 格式化Long类型时间
     */
    public static String getTime(Long time) {
        try {
            //格式化时间
            Date date = new Date(time);
            return new SimpleDateFormat("yyyy/M/d HH:mm:ss").format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    /***
     * 格式化Long类型时间
     */
    public static String getTime(Long time, String format) {
        try {
            //格式化时间
            Date date = new Date(time);
            return new SimpleDateFormat(format).format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /***
     * 格式化的时间
     */
    public static String formatWalletTime(String time) {
        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
            return sdf2.format(sdf1.parse(time));
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /***
     * 按格式,格式化的时间
     */
    public static String formatTime(String time, String oldFormat, String newFormat) {
        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat(oldFormat);
            SimpleDateFormat sdf2 = new SimpleDateFormat(newFormat);
            return sdf2.format(sdf1.parse(time));
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /***
     * 获取当前格式化的时间
     */
    public static String getCurrentTime() {
        try {
            Date date = new Date(System.currentTimeMillis());
            return new SimpleDateFormat("yyyy-M-d HH:mm").format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /***
     * 获取当前格式化的时间
     */
    public static String getCurrentTime(String pattern) {
        try {
            Date date = new Date(System.currentTimeMillis());
            return new SimpleDateFormat(pattern).format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /***
     * 将时间转化为毫秒
     */
    public static long timeToMsec(String time) {
        long msec = 0;
        try {
            long days = Integer.parseInt(time.split(":")[0]) * 24 * 60 * 60;
            long hours = Integer.parseInt(time.split(":")[1]) * 60 * 60;
            long minutes = Integer.parseInt(time.split(":")[2]) * 60;
            long second = Integer.parseInt(time.split(":")[3]);
            msec = days + hours + minutes + second;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msec * 1000;
    }

    /***
     * 将毫秒转化为时间
     */
    public static String msecToTime(long msec) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = msec / dd;
        Long hour = (msec - day * dd) / hh;
        Long minute = (msec - day * dd - hour * hh) / mi;
        Long second = (msec - day * dd - hour * hh - minute * mi) / ss;

        StringBuilder sb = new StringBuilder();
        if (day > 0) {
            sb.append(day + "天");
        }
        if (hour > 0) {
            sb.append(hour + "时");
        }
        if (minute > 0) {
            if (day <= 0) {
                sb.append(minute + "分");
            }
        }
        if (second > 0) {
            if (day <= 0 && hour <= 0) {
                sb.append(second + "秒");
            }
        }
        return sb.toString();
    }


    /**
     * 日期月份减1个月
     *
     * @param str 时间格式  2019-04-01
     */
    public static String monthSub(String str) {
        try {
            int year = Integer.parseInt(str.split("-")[0]);
            int month = Integer.parseInt(str.split("-")[1]);
            if (month == 1) {
                //当前为1月份,减一个月就为上一年的12月份
                month = 12;
                year = year - 1;
            } else {
                month = month - 1;
            }
            return year + "-" + month + "-01";
        } catch (Exception e) {
            e.printStackTrace();
            return str;
        }
    }

    /**
     * 日期加几月
     */
    public static String monthAdd(String str) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date dt = sdf.parse(str);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, 1);// 日期加3个月
        // rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天
        Date dt1 = rightNow.getTime();
        return sdf.format(dt1);
    }

    /**
     * 获取日期前后的某一天
     * @param strDate
     * @param distanceDay
     * @return
     */
    public static String getDate(String strDate, int distanceDay) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-M-d");
        Date beginDate = null;
        DateFormat df = new SimpleDateFormat("yyyy-M-d");
        try {
            beginDate = df.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }

    /**
     * 判断2个时间大小
     * yyyy-MM-dd HH:mm 格式（自己可以修改成想要的时间格式）
     * @param startTime
     * @param endTime
     * @return
     */
    public static int timeCompare(String startTime, String endTime){
        int i=0;
        //注意：传过来的时间格式必须要和这里填入的时间格式相同
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1 = dateFormat.parse(startTime);//开始时间
            Date date2 = dateFormat.parse(endTime);//结束时间
            // 1 结束时间小于开始时间 2 开始时间与结束时间相同 3 结束时间大于开始时间
            if (date2.getTime()<date1.getTime()){
                //结束时间小于开始时间
                i= 1;
            }else if (date2.getTime()==date1.getTime()){
                //开始时间与结束时间相同
                i= 2;
            }else if (date2.getTime()>date1.getTime()){
                //结束时间大于开始时间
                i= 3;
            }
        } catch (Exception e) {

        }
        return  i;
    }

    /**
     * 日期逻辑
     * @param dateStr 日期字符串
     * @return
     */
    public static String timeLogic(String dateStr, String format, String newFormat) {
        Calendar calendar = Calendar.getInstance();
        calendar.get(Calendar.DAY_OF_MONTH);
        long now = calendar.getTimeInMillis();
        Date date = strToDate(dateStr, format);
        calendar.setTime(date);
        long past = calendar.getTimeInMillis();

        // 相差的秒数
        long time = (now - past) / 1000;

        StringBuffer sb = new StringBuffer();
        if (time > 0 && time < 60) { // 1小时内
            return sb.append(time + "秒前").toString();
        } else if (time > 60 && time < 3600) {
            return sb.append(time / 60 + "分钟前").toString();
        } else if (time >= 3600 && time < 3600 * 24) {
            return sb.append(time / 3600 + "小时前").toString();
        } else if (time >= 3600 * 24 && time < 3600 * 48) {
            return sb.append("昨天").toString();
        } else if (time >= 3600 * 48 && time < 3600 * 72) {
            return sb.append("前天").toString();
        } else if (time >= 3600 * 72) {
            return formatTime(dateStr, newFormat);
        }
        return formatTime(dateStr, newFormat);
    }

    /**
     * 日期字符串转换为Date
     * @param dateStr
     * @param format
     * @return
     */
    public static Date strToDate(String dateStr, String format) {
        Date date = null;

        if (!TextUtils.isEmpty(dateStr)) {
            DateFormat df = new SimpleDateFormat(format);
            try {
                date = df.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    /**
     * 将秒转换成00′25″
     * @param second
     * @return
     */
    public static String getTime(int second) {
        if (second < 10) {
            return "00′0" + second + "″";
        }
        if (second < 60) {
            return "00′" + second + "″";
        }
//        if (second < 3600) {
            int minute = second / 60;
            second = second - minute * 60;
            if (minute < 10) {
                if (second < 10) {
                    return "0" + minute + "′0" + second + "″";
                }
                return "0" + minute + "′" + second + "″";
            }
            if (second < 10) {
                return minute + "′0" + second + "″";
            }
            return minute + "′" + second + "″";
//        }
//        int hour = second / 3600;
//        int minute = (second - hour * 3600) / 60;
//        second = second - hour * 3600 - minute * 60;
//        if (hour < 10) {
//            if (minute < 10) {
//                if (second < 10) {
//                    return "0" + hour + ":0" + minute + ":0" + second;
//                }
//                return "0" + hour + ":0" + minute + ":" + second;
//            }
//            if (second < 10) {
//                return "0" + hour + minute + ":0" + second;
//            }
//            return "0" + hour + minute + ":" + second;
//        }
//        if (minute < 10) {
//            if (second < 10) {
//                return hour + ":0" + minute + ":0" + second;
//            }
//            return hour + ":0" + minute + ":" + second;
//        }
//        if (second < 10) {
//            return hour + minute + ":0" + second;
//        }
//        return hour + minute + ":" + second;
    }

    /**
     * 获取前n天日期、后n天日期
     *
     * @param distanceDay 前几天 如获取前7天日期则传-7即可；如果后7天则传7
     * @return
     */
    public static String getOldDate(int distanceDay) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }
}
